package com.msc.serclet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.testExport;

/**
 * Servlet implementation class ExportExcel
 */
@WebServlet(urlPatterns = "/export")
public class ExportExcel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExportExcel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		testExport t = new testExport();
		t.test();
		//获取请求文件名
		//String fileName = request.getParameter("fileName");
		String fileName = "你好.xls";
		fileName=new String(fileName.getBytes("UTF-8"),"GBK");
		System.out.println(fileName);
		//设置文件MIME类型（互联网邮件拓展类型）
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		//读取目标文件，通过response将目标文件写到客户端
		//获取目标文件的绝对路径
		String fullFileName = "/usr/" + fileName;
		//读取文件
		InputStream in = new FileInputStream(fullFileName);
		OutputStream out = response.getOutputStream();
		
		//写文件
		int b;
		while((b = in.read()) != -1){
			out.write(b);
			
		}
		in.close();
		out.close();
	}

}
